$(function() {
	//设置活动按钮
	changeHeaderMenu(0);
	changeMenu(0, 2);
	//初始化列表数据
	initTable();
	savehandle();
});

function savehandle() {
	$('#alertdiv').hide();
	$('#alertdiv').savehandle({
		fieldMap : {},
		successMsg : "成功！",
		failMsg : "失败，请重试！",
		success : refresh
	});
}

/**
 * 初始化表格数据
 */
function initTable() {
	$('#grid').grid({
		title : '',
		pager : true,
		store : {
			url : '/fw/agent/userbind_getmybinduser.action',
			idProperty : 'agentIndex',
			uploadPagesize : 'rows'
		},
		pageCfg : {
			btnCount : 5
		},
		columns : [
		        {cn:'用户名', en:'agentName', width : 150},
		        {cn:'累积充值点数', en:'pointTotal', width : 100},
		        {cn:'本月新增点数', en:'pointTotalMonth',width : 100},
		        {cn:'剩余点数', en:'agentPoint',width : 100},
		        {cn:'操作', en:'agentIndex',width : 200, tpl: function(r) {
		        	var html = '';
        			html += '<button onclick="consumeDetail(' + r.agentIndex + ')" class="btn btn-success btn-xs">消费详情</button>';
        			html += '&nbsp;&nbsp;';
        			html += '<button onclick="rechargeFind(' + r.agentIndex + ', \'' + r.agentName + '\')" class="btn btn-success btn-xs">充值查询</button>';
        			return html;
		        }}
				]
	});
}

function consumeDetail(id) {
	location.href = 'recOfConsList.jsp?agentIndex=' + id;
}

function rechargeFind(id, name) {
	$('#agentIndex').val(id);
	$('#agentName').html(name);
	$('#startDate').val('');
	$('#endDate').val('');
	$('#edit_result').html('');
	$('#editModal').modal('show');
}

function rechargeFindOk() {
	var agentIndex = $('#agentIndex').val();
	var startDate = $('#startDate').val();
	var endDate = $('#endDate').val();
	var data = {
		agentIndex: agentIndex,
		type: 3
	};
	if(startDate) {
		data.startDate = startDate.replace("-", "").replace("-", "").replace(" ", "").replace(":", "").replace(":", "");
	}
	if(endDate) {
		data.endDate = endDate.replace("-", "").replace("-", "").replace(" ", "").replace(":", "").replace(":", "");
	}
	$.getJSON('/fw/agent/agentCon_gettotal.action', data, function(data) {
		if(data.success && data.result) {
			$('#edit_result').html(data.result + '点');
		} else {
			$('#edit_result').html('查询失败');
		}
	});
}

/**
 * 刷新
 * 
 * @return {TypeName}
 */
function refresh() {
	$('#grid').grid('load', {data : {}});
}